﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_Applications : System.Web.UI.Page
{
    rUser ruser;
    DataSet permisos;
    Table tablaPanel = null;
    TableRow rowPage;
    RE_GenericBean rpage = new RE_GenericBean();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null) { Response.Redirect("../Default.aspx"); }

        //Si es la primera vez que entra
        if (!Page.IsPostBack)
        {
            rpage.intC1 = 0;
            rpage.intC2 = 0;
            Session["rpageapp"] = rpage;

            string rxml = null;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            rxml = service.getOpcionApp(1);

            if (!rxml.Equals("") || rxml != null)
            {
                permisos = Utility.getOpcionesbyApp(rxml);
                Session["pagespermit"] = permisos;
            }

            ruser = (rUser)Session["userid"];

            pintarOpciones(0, 0);
        }
        else
        {
            rpage = (RE_GenericBean)Session["rpageapp"];
            if (rpage != null)
            {
                pintarOpciones(rpage.intC1, rpage.intC2);
            }
        }
        restricted();
        permiso();
    }

    public void pintarOpciones(int pgnivel, int pgnum)
    {
        try
        {
            string rxml2 = null;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            rxml2 = service.getPagesView(1);

            if (!rxml2.Equals("") || rxml2 != null)
            {
                DataSet dsPages = Utility.getPagesView(rxml2);
                //Session["dsPagesView"] = dsPages;
                string pagop = service.getAppPages(1);

                if (!pagop.Equals("") || pagop != null)
                {
                    DataSet dsclient = Utility.getPermitPage(pagop);
                    if (pagop != null)
                    {
                        if (dsclient.Tables[0].Columns.Count != 0)
                        {
                            Panel1.Controls.Clear();
                            tablaPanel = new Table();
                            tablaPanel.Width = 720;
                            tablaPanel.CellPadding = 5;
                            tablaPanel.HorizontalAlign = HorizontalAlign.Left;
                            tablaPanel.Font.Size = 9;
                            tablaPanel.Font.Name = "Arial";
                            tablaPanel.BorderStyle = BorderStyle.Ridge;
                            tablaPanel.BorderColor = System.Drawing.Color.Black;
                            tablaPanel.BorderWidth = 3;
                            tablaPanel.CssClass = "bodyPag";

                            if (pgnivel == 0)
                            {
                                for (int j = 0; j < dsPages.Tables[0].Rows.Count; j++)
                                {
                                    if (int.Parse(dsPages.Tables[0].Rows[j]["type"].ToString()) == 1)
                                    {
                                        for (int i = 0; i < dsclient.Tables[0].Rows.Count; i++)
                                        {
                                            if (int.Parse(dsPages.Tables[0].Rows[j]["page_id"].ToString()) == int.Parse(dsclient.Tables[0].Rows[i]["id_page"].ToString()))
                                            {
                                                loadImaPages((j + 1), dsclient.Tables[0].Rows[i]["id_page"].ToString(), dsclient.Tables[0].Rows[i]["name_page"].ToString(), dsclient.Tables[0].Rows[i]["image"].ToString());
                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        loadImaPages((j + 1), ("1_" + dsPages.Tables[0].Rows[j]["id"].ToString()), dsPages.Tables[0].Rows[j]["name"].ToString(), dsPages.Tables[0].Rows[j]["url"].ToString());
                                    }
                                }
                            }
                            else
                            {
                                int contp = 0;
                                for (int j = 0; j < dsPages.Tables[pgnivel].Rows.Count; j++)
                                {
                                    string pageid = "pg" + pgnivel + "_id";
                                    if (int.Parse(dsPages.Tables[pgnivel].Rows[j][pageid].ToString()) == pgnum)
                                    {
                                        contp++;
                                        if (int.Parse(dsPages.Tables[pgnivel].Rows[j]["type"].ToString()) == 1)
                                        {
                                            for (int i = 0; i < dsclient.Tables[0].Rows.Count; i++)
                                            {
                                                if (int.Parse(dsPages.Tables[pgnivel].Rows[j]["page_id"].ToString()) == int.Parse(dsclient.Tables[0].Rows[i]["id_page"].ToString()))
                                                {
                                                    loadImaPages(contp, dsclient.Tables[0].Rows[i]["id_page"].ToString(), dsclient.Tables[0].Rows[i]["name_page"].ToString(), dsclient.Tables[0].Rows[i]["image"].ToString());
                                                    break;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            loadImaPages(contp, ("2_" + dsPages.Tables[pgnivel].Rows[j]["id"].ToString()), dsPages.Tables[pgnivel].Rows[j]["name"].ToString(), dsPages.Tables[pgnivel].Rows[j]["url"].ToString());
                                        }
                                    }
                                }
                            }
                        }
                        tablaPanel.Attributes.Add("Style", "box-shadow: 0 0 20px 3px #000000;");
                        Panel1.Controls.Add(tablaPanel);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Utility.writeError("Error al obtener datos paginas WEBSERVICE maplication.aspx " + ex.Message + " " + ex.StackTrace);
        }
    }

    private void loadImaPages(int contPage, string idPage, string namePage, string imaPage)
    {
        if (contPage == 1)
        {
            rowPage = new TableRow();
            tablaPanel.Controls.Add(rowPage);
        }
        if ((contPage % 4) == 0)
        {
            rowPage.Cells.Add(oldRowIma(contPage, idPage, namePage, imaPage));
            tablaPanel.Controls.Add(rowPage);
            rowPage = new TableRow();
        }
        else
        {
            rowPage.Cells.Add(oldRowIma(contPage, idPage, namePage, imaPage));
            tablaPanel.Controls.Remove(rowPage);
            tablaPanel.Controls.Add(rowPage);
        }
    }

    private TableCell oldRowIma(int contPage, string idPage, string namePage, string imaPage)
    {
        TableCell result = new TableCell();

        Table table = new Table();
        TableRow row1 = new TableRow();
        TableCell cell1 = new TableCell();
        cell1.Width = 120;
        cell1.Height = 65;
        cell1.HorizontalAlign = HorizontalAlign.Center;
        ImageButton Buttons = new ImageButton();
        Buttons.ID = "bt_" + idPage;
        Buttons.Click += new ImageClickEventHandler(this.Buttons_Click);

        //Buttons.BorderWidth = 0;

        string auxOver = imaPage.Replace(".", "2.");
        Buttons.Attributes.Add("onmouseout", "this.src='../img/ImaPages/" + imaPage + "'");
        Buttons.Attributes.Add("onmouseover", "this.src='../img/ImaPages/" + auxOver + "'");

        if (!imaPage.Equals(""))
        {
            Buttons.ImageUrl = "../img/ImaPages/" + imaPage;
        }
        cell1.Attributes.Add("Style", "background: url(../img/ImaPages/base.png) no-repeat center bottom;");
        
        cell1.Controls.Add(Buttons);
        row1.Cells.Add(cell1);
        table.Controls.Add(row1);

        TableRow row2 = new TableRow();

        TableCell cell2 = new TableCell();
        cell2.Width = 120;
        cell2.Height = 50;
        cell2.HorizontalAlign = HorizontalAlign.Center;
        cell2.VerticalAlign = VerticalAlign.Top;
        Label imaLabel = new Label();
        imaLabel.Text = namePage;
        imaLabel.Font.Bold = true;
        cell2.Controls.Add(imaLabel);
        row2.Cells.Add(cell2);
        table.Controls.Add(row2);

        result.Controls.Add(table);
        return result;
    }

    private void Buttons_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton ibt = (ImageButton)sender;
        string ID = ibt.ID;
        string[] pID = ID.Split('_');
        if (pID.Length < 3)
        {
            Response.Redirect("application.aspx?id=" + pID[1]);
        }
        else
        {
            rpage.intC1 = int.Parse(pID[1]);
            rpage.intC2 = int.Parse(pID[2]);
            Session["rpageapp"] = rpage;
            pintarOpciones(rpage.intC1, rpage.intC2);
            lnk_Back.Visible = true;
        }
    }

    protected void lnk_Back_Click(object sender, EventArgs e)
    {
        rpage.intC1 = 0; rpage.intC2 = 0;
        Session["rpageapp"] = rpage;
        Response.Redirect(Request.UrlReferrer.ToString());
    }

    public void restricted()
    {
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
            // permiso = long.Parse(user.getRestricted().ToString());
            RE_GenericBean permi = Utility.getPermisobyRuser(user, 13);
            permiso = permi.int64C2;
        }
        if ((permiso & 1) == 1) { Response.Redirect(Request.UrlReferrer.ToString()); }
    }

    public void permiso()
    {
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
            //permiso = long.Parse(user.getPermisoUser().ToString());
            RE_GenericBean permi = Utility.getPermisobyRuser(user, 13);
            permiso = permi.int64C1;
        }
        //if ((permiso & 1) != 1) { Panel1.Enabled = false; tb_application.Enabled = false; lnk_add.Enabled = false; }
    }
    protected void lnk_add_Click(object sender, EventArgs e)
    {
        if (!tb_application.Text.Equals(""))
        {
            //primero obtengo el valor de permiso que toca
            Label1.Text = "";
            string sxml = "";
            sxml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <opcion><name>";
            sxml += tb_application.Text + "</name>";
            sxml += "<permiso>1</permiso>";
            sxml += "<app>1</app>";
            sxml += "</opcion>";
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            int pe = service.registraPage(sxml);
            if (pe == 1)
            {
                Label1.Text = "Page " + tb_application.Text + " added ";
                tb_application.Text = "";
                string op = service.getOpcionApp(1);
                pintarOpciones(0, 0);
            }
        }
        else
            Label1.Text = "Required Field";
    }
}